package cn.com.enersun.iec61850.web.mapper;

import cn.com.enersun.iec61850.web.domain.ServerModelStructure;
import org.apache.ibatis.annotations.*;
import org.openmuc.openiec61850.Fc;

import java.util.List;

/**
 * Created by YYC on 2017/5/4.
 */
@Mapper
public interface ServerModelStructureMapper {
    //指定类属性与数据库字段对应关系
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "devName", column = "devName"),
            @Result(property = "moduleName", column = "moduleName"),
            @Result(property = "parentReference", column = "parentReference"),
            @Result(property = "hasChild", column = "hasChild"),
            @Result(property = "nodeName", column = "nodeName"),
            @Result(property = "nodeClass", column = "nodeClass"),
            @Result(property = "nodeFc", column = "nodeFc"),
    })

    @Select("SELECT devName, moduleName, parentReference, hasChild, nodeName, nodeClass, nodeFc from servermodels WHERE devName = #{devName} AND parentReference = #{parentReference}")
    ServerModelStructure selectServerModelStructure(String devName, String parentReference);

    //传入两个参数，调用此方法时会报上边的错，提示你传入的参数找不到。
    //解决方法 在参数前加上@Param注解,如果不使用该注解，那么传入的参数只能有一个(以这种传参方式的话)
    @Select("SELECT devName, moduleName, parentReference, hasChild, nodeName, nodeClass, nodeFc from servermodels WHERE devName = #{devName} AND moduleName = #{moduleName} AND nodeClass = #{nodeClass}")
    List<ServerModelStructure> selectModelByNodeClass(@Param("devName")String devName, @Param("moduleName")String moduleName, @Param("nodeClass")String nodeClass);

    @Select("SELECT devName, moduleName, parentReference, hasChild, nodeName, nodeClass, nodeFc from servermodels WHERE devName = #{devName}  AND  ISNULL(parentReference)")
    List<ServerModelStructure> selectModelRoot(String devName);

    @Select("SELECT devName, moduleName, parentReference, hasChild, nodeName, nodeClass, nodeFc from servermodels WHERE devName = #{devName}  AND moduleName = 'DataSets' AND nodeFc IS NOT NULL")
    List<ServerModelStructure> selectDataSetDoByNodeFc(String devName);

    @Select("SELECT devName, moduleName, parentReference, hasChild, nodeName, nodeClass, nodeFc from servermodels WHERE devName = #{devName} AND ISNULL(hasChild)")
    List<ServerModelStructure> selectModelLeaf(@Param("devName")String devName);

    @Insert("INSERT into servermodels(id, devName, moduleName, parentReference, hasChild, nodeName, nodeClass, nodeFc) VALUES(#{id}, #{devName}, #{moduleName}, #{parentReference}, #{hasChild}, #{nodeName}, #{nodeClass}, #{nodeFc})")
    //在执行INSERT语句之前给id属性自动生成UUID值
    @SelectKey(statement="select replace(uuid(),'-','')", keyProperty="id", resultType=String.class, before=true)
    int insertServerModelValue(ServerModelStructure serverModelValue);


    @Delete("DELETE FROM servermodels WHERE devName = #{devName} AND moduleName = #{moduleName} AND selfNode = #{nodeName}")
    void deleteServerModelValue(String devName, String moduleName, String nodeName);
}
